//
//  TestMarqueeLabel.m
//  MKUITestApp
//  
//  Created by 苏尚进 on 2025/9/23.
//  Copyright © 2025 moon. All rights reserved.
//

#import "MKUIMarqueeView.h"

@interface MKUIMarqueeView()
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,assign) BOOL isAnimating;
@property(nonatomic,strong) MKUIAnimation *marqueeAnimation;
@end

@implementation MKUIMarqueeView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.enableAnimation = YES;
        self.speed = 40;
        self.scrollView = [[UIScrollView alloc] init];
        self.scrollView.scrollEnabled = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self addSubview:self.scrollView];
    }
    return self;
}
- (void)setContentView:(UIView *)contentView{
    if(_contentView==contentView)return;
    if(_contentView) [_contentView removeFromSuperview];
    _contentView = contentView;
    if(_contentView) [self.scrollView addSubview:_contentView];
    [self setNeedsLayout];
}
- (void)setContentInsets:(UIEdgeInsets)contentInsets{
    if(UIEdgeInsetsEqualToEdgeInsets(_contentInsets, contentInsets))return;
    _contentInsets = contentInsets;
    [self setNeedsLayout];
    if(self.isAnimating){
        [self refreshContentView];
    }
}
- (void)setSpeed:(CGFloat)speed{
    if(speed<=0)return;
    if(_speed==speed)return;
    _speed = speed;
    if(self.isAnimating){
        [self refreshContentView];
    }
}
- (void)setEnableAnimation:(BOOL)enableAnimation{
    if(_enableAnimation==enableAnimation)return;
    _enableAnimation = enableAnimation;
    [self refreshContentView];
}
- (void)setAnimationBeginFromEmtpy:(BOOL)animationBeginFromRightEdge{
    if(_animationBeginFromRightEdge==animationBeginFromRightEdge)return;
    _animationBeginFromRightEdge = animationBeginFromRightEdge;
    [self refreshContentView];
}
- (CGSize)sizeThatFits:(CGSize)size{
    if(!self.contentView) return CGSizeZero;
    UIEdgeInsets insets = self.contentInsets;
    CGSize s = [self.contentView sizeThatFits:CGSizeMake(size.width-insets.left-insets.right, size.height-insets.top-insets.bottom)];
    if(CGSizeEqualToSize(s, CGSizeZero)) return CGSizeZero;
    if(s.width>0) s.width += insets.left+insets.right;
    if(s.height>0) s.height += insets.top+insets.bottom;
    if(!self.enableAnimation){
        s.width = MIN(s.width,size.width);
    }
    return s;
}
- (CGSize)intrinsicContentSize{
    return [self sizeThatFits:CGSizeMake(9999999, 9999999)];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    UIEdgeInsets insets = self.contentInsets;
    CGRect innerBounds = UIEdgeInsetsInsetRect(bounds, insets);
    
    CGRect f1 = innerBounds;
    self.scrollView.frame = f1;
    
    f1.size = [self.contentView sizeThatFits:CGSizeMake(999999, innerBounds.size.height)];
    if(!self.enableAnimation){
        f1.size.width = MIN(f1.size.width,innerBounds.size.width);
    }
    f1.size.height = innerBounds.size.height;
    f1.origin = CGPointZero;
    self.contentView.frame = f1;
    
    CGSize contentSize = self.contentView.bounds.size;
    self.scrollView.contentSize = contentSize;
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    BOOL needMarquee = [self needMarquee];
    if(needMarquee){//需要跑马灯
        contentInsets.left = innerBounds.size.width;
        contentInsets.right = innerBounds.size.width;
    }
    self.scrollView.contentInset = contentInsets;
    if(!needMarquee){
        self.scrollView.contentOffset = CGPointZero;
    }else{
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = MIN(offset.x,self.scrollView.mk_contentOffsetOfMaxX);
        offset.x = MAX(offset.x,self.scrollView.mk_contentOffsetOfMinX);
        self.scrollView.contentOffset = offset;
    }
    if(needMarquee){
        [self stopAnimation];
        [self beginAnimation];
    }else{
        [self stopAnimation];
    }
}
- (BOOL)needMarquee{
    BOOL needMarquee = self.scrollView.bounds.size.width<self.scrollView.contentSize.width;
    needMarquee &= self.enableAnimation;
    return needMarquee;
}
- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    if(!self.superview){
        [self stopAnimation];
    }else{
        [self refreshContentView];
    }
}
- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
    if(hidden){
        [self stopAnimation];
    }else{
        [self refreshContentView];
    }
}
- (void)stopAnimation{
    if(!self.isAnimating) return;
    self.isAnimating = NO;
    [self.marqueeAnimation cancelAnimation];
}
- (void)beginAnimation{
    if(self.isAnimating) return;
    if(self.marqueeAnimation.isAnimation){
        [self.marqueeAnimation cancelAnimation];
    }
    BOOL needMarquee = [self needMarquee];
    if(!needMarquee) return;
    
    self.isAnimating = YES;
    if(!self.marqueeAnimation){
        self.marqueeAnimation = [[MKUIAnimation alloc] init];
    }
    
    CGSize contentSize = self.scrollView.contentSize;
    UIEdgeInsets contentInsets = self.scrollView.contentInset;
    CGFloat len = contentSize.width+contentInsets.right;
    
    self.marqueeAnimation.duration = len/self.speed;
    self.marqueeAnimation.repeatCount = 0;//无限
    if(self.animationBeginFromRightEdge){
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = contentSize.width;
        self.scrollView.contentOffset = offset;
    }
    CGPoint startOffset = self.scrollView.contentOffset;
    @MK_WEAKIFY(self);
    self.marqueeAnimation.animationsBlock = ^(CGFloat progress) {
        @MK_NORMALIZE(self);
        CGPoint offset = CGPointZero;
        offset.x = startOffset.x+len*progress;
        if(offset.x>contentSize.width){
            offset.x = -contentInsets.left+offset.x-contentSize.width;
        }
        self.scrollView.contentOffset = offset;
    };
    [self.marqueeAnimation beginAnimation];
}
- (void)refreshContentView{
    [self setNeedsLayout];
}
@end

@interface MKUIMarqueeLabel()
@property(nonatomic,strong) UILabel *textLabel;
@end
@implementation MKUIMarqueeLabel
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.numberOfLines = 1;
        self.contentView = self.textLabel;
    }
    return self;
}
- (NSString *)text{
    return self.textLabel.text;
}
- (void)setNumberOfLines:(NSInteger)numberOfLines{
    self.textLabel.numberOfLines = numberOfLines;
}
- (NSInteger)numberOfLines{
    return self.textLabel.numberOfLines;
}
- (void)setText:(NSString *)text{
    if(self.enableAnimation){
        //需要将\n转换为空格
        text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
        text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@" "];
    }
    if(self.text==text||[self.text isEqualToString:text])return;
    self.textLabel.text = text;
    [self refreshContentView];
}
- (NSAttributedString *)attributedText{
    return self.textLabel.attributedText;
}
- (void)setAttributedText:(NSAttributedString *)attributedText{
    if(self.attributedText==attributedText||[self.attributedText isEqualToAttributedString:attributedText])return;
    self.textLabel.attributedText = attributedText;
    [self refreshContentView];
}
- (UIColor *)textColor{
    return self.textLabel.textColor;
}
- (void)setTextColor:(UIColor *)textColor{
    self.textLabel.textColor = textColor;
}
- (UIFont *)font{
    return self.textLabel.font;
}
- (void)setFont:(UIFont *)font{
    if(self.font==font||[self.font isEqual:font])return;
    self.textLabel.font = font;
    [self refreshContentView];
}
@end
